#!/usr/bin/env python """ Example of a simple embedded engine in a tkinter python application. """ import Tkinter as tk from ptk_lib.engine import tk_engine, eng_misc class TestApp(): def __init__(self, root): self.root = root #create a toplevel window self.frame = tk.Toplevel() #use the __main__ namespace (this file) as the top #level. import __main__ usr_dict = __main__.__dict__ #Alternatively this could be an empty dictionary in which you #can store objects you want to expose to the engine. usr_dict = {} usr_dict['a'] = 1 #expose objects to the engine. usr_dict['b'] = 'test string' #create the engine object # parent - the parent wxpython object used for events # engine label - The label display to the user on the console tab in # PTK # userdict - the local dictionary for the top level namespace self.eng = tk_engine.TkEngine( self.root,'TkTestApp', usr_dict) #bind to the engine close events self.root.bind(tk_engine.EvtDisconnect, self.on_engine_disconnect) #make sure we close the engine when exiting. self.frame.protocol("WM_DELETE_WINDOW", self.on_frame_close) #connect to a running PTK instance port = eng_misc.get_message_port() self.eng.connect( 'localhost', port) def on_frame_close(self): self.eng.disconnect() self.frame.destroy() self.root.quit() def on_engine_disconnect(self, event): print 'Engine disconnected' root = tk.Tk() root.withdraw() app = TestApp(root) root.mainloop()